/*
 *  (C) Copyright 2007 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Jazz.
 *
 *  Jazz is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Jazz is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.jazz.resolver;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.text.JTextComponent;

import nextapp.echo2.app.CheckBox;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.ListBox;
import nextapp.echo2.app.RadioButton;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.text.TextComponent;
import nextapp.echo2.extras.app.CalendarSelect;

import org.openinsula.jazz.resolver.echo2.DefaultCheckBoxResolver;
import org.openinsula.jazz.resolver.echo2.DefaultLabelResolver;
import org.openinsula.jazz.resolver.echo2.DefaultListBoxResolver;
import org.openinsula.jazz.resolver.echo2.DefaultRadioButtonResolver;
import org.openinsula.jazz.resolver.echo2.DefaultSelectFieldResolver;
import org.openinsula.jazz.resolver.echo2.DefaultTextComponentResolver;
import org.openinsula.jazz.resolver.echo2extras.DefaultCalendarSelectResolver;
import org.openinsula.jazz.resolver.echopointng.DefaultDateFieldResolver;
import org.openinsula.jazz.resolver.itmill.AbstractFieldResolver;
import org.openinsula.jazz.resolver.itmill.TextFieldResolver;
import org.openinsula.jazz.resolver.swing.DefaultJCheckBoxResolver;
import org.openinsula.jazz.resolver.swing.DefaultJComboBoxResolver;
import org.openinsula.jazz.resolver.swing.DefaultJLabelResolver;
import org.openinsula.jazz.resolver.swing.DefaultJListResolver;
import org.openinsula.jazz.resolver.swing.DefaultJPasswordFieldResolver;
import org.openinsula.jazz.resolver.swing.DefaultJRadioButtonResolver;
import org.openinsula.jazz.resolver.swing.DefaultJSpinnerResolver;
import org.openinsula.jazz.resolver.swing.DefaultJTextComponentResolver;
import org.springframework.util.ClassUtils;

import com.itmill.toolkit.ui.AbstractField;
import com.itmill.toolkit.ui.TextField;

import echopointng.DateField;

public class DefaultResolverRegistry implements ResolverRegistry {

	protected Map<Class<?>, Resolver> resolverMap = new HashMap<Class<?>, Resolver>();

	public DefaultResolverRegistry() {
		resolverMap.put(JCheckBox.class, new DefaultJCheckBoxResolver());
		resolverMap.put(JComboBox.class, new DefaultJComboBoxResolver());
		resolverMap.put(JLabel.class, new DefaultJLabelResolver());
		resolverMap.put(JList.class, new DefaultJListResolver());
		resolverMap.put(JPasswordField.class, new DefaultJPasswordFieldResolver());
		resolverMap.put(JRadioButton.class, new DefaultJRadioButtonResolver());
		resolverMap.put(JSpinner.class, new DefaultJSpinnerResolver());
		resolverMap.put(JTextComponent.class, new DefaultJTextComponentResolver());

		if (ClassUtils.isPresent("nextapp.echo2.app.text.TextComponent")) {
			resolverMap.putAll(Echo2ResolverMapFactory.getResolverMap());
		}
		if (ClassUtils.isPresent("nextapp.echo2.extras.app.CalendarSelect")) {
			resolverMap.putAll(Echo2ExtrasResolverMapFactory.getResolverMap());
		}
		if (ClassUtils.isPresent("echopointng.DateField")) {
			resolverMap.putAll(EchoPointNGResolverMapFactory.getResolverMap());
		}
		if (ClassUtils.isPresent("com.itmill.toolkit.ui.TextField")) {
			resolverMap.putAll(ItMillResolverMapFactory.getResolverMap());
		}
	}

	@Override
	public Resolver getResolverForType(Class<?> beanType) {
		Class<?> iterateClass = beanType;
		do {
			Resolver resolver = resolverMap.get(iterateClass);
			if (resolver != null) {
				return resolver;
			}
			iterateClass = iterateClass.getSuperclass();
		} while (iterateClass != Object.class);

		throw new IllegalArgumentException("No Resolver found for type " + beanType.getCanonicalName());
	}

	private static class Echo2ResolverMapFactory {
		public static Map<Class<?>, Resolver> getResolverMap() {
			Map<Class<?>, Resolver> resolverMap = new HashMap<Class<?>, Resolver>();

			resolverMap.put(CheckBox.class, new DefaultCheckBoxResolver());
			resolverMap.put(Label.class, new DefaultLabelResolver());
			resolverMap.put(ListBox.class, new DefaultListBoxResolver());
			resolverMap.put(RadioButton.class, new DefaultRadioButtonResolver());
			resolverMap.put(SelectField.class, new DefaultSelectFieldResolver());
			resolverMap.put(TextComponent.class, new DefaultTextComponentResolver());

			return resolverMap;
		}
	}

	private static class Echo2ExtrasResolverMapFactory {
		public static Map<Class<?>, Resolver> getResolverMap() {
			Map<Class<?>, Resolver> resolverMap = new HashMap<Class<?>, Resolver>();

			resolverMap.put(CalendarSelect.class, new DefaultCalendarSelectResolver());

			return resolverMap;
		}
	}

	private static class EchoPointNGResolverMapFactory {
		public static Map<Class<?>, Resolver> getResolverMap() {
			Map<Class<?>, Resolver> resolverMap = new HashMap<Class<?>, Resolver>();

			resolverMap.put(DateField.class, new DefaultDateFieldResolver());

			return resolverMap;
		}
	}

	private static class ItMillResolverMapFactory {
		public static Map<Class<?>, Resolver> getResolverMap() {
			Map<Class<?>, Resolver> resolverMap = new HashMap<Class<?>, Resolver>();

			resolverMap.put(TextField.class, new TextFieldResolver());
			resolverMap.put(AbstractField.class, new AbstractFieldResolver());

			return resolverMap;
		}
	}

}
